package com.stud.seckill.controller;

import cn.hutool.core.bean.BeanUtil;
import com.stud.seckill.common.auth.UserContext;
import com.stud.seckill.entity.vo.GoodsDetailVo;
import com.stud.seckill.entity.GoodsEntity;
import com.stud.seckill.entity.SeckillGoodsEntity;
import com.stud.seckill.entity.UserEntity;
import com.stud.seckill.service.GoodsService;
import com.stud.seckill.service.SeckillGoodsService;
import com.stud.seckill.entity.vo.GoodsVo;
import com.stud.seckill.common.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @GetMapping("/toList")
    public String toList(Model model, GoodsEntity goods) {

        // 查询商品列表
        List<GoodsEntity> goodsList = goodsService.selectList(goods);
        List<SeckillGoodsEntity> seckillGoodsList = seckillGoodsService.selectList(new SeckillGoodsEntity());

        List<GoodsVo> goodsVoList = BeanUtil.copyToList(goodsList, GoodsVo.class);
        for (GoodsVo goodsVo : goodsVoList) {
            SeckillGoodsEntity seckillGoods = seckillGoodsList.stream().filter(data -> data.getGoodsId().equals(goodsVo.getId())).findFirst().orElse(new SeckillGoodsEntity());
            goodsVo.setStartDate(seckillGoods.getStartDate());
            goodsVo.setEndDate(seckillGoods.getEndDate());
            goodsVo.setSeckillPrice(seckillGoods.getSeckillPrice());
            goodsVo.setStockCount(seckillGoods.getStockCount());
        }

        model.addAttribute("goodsList", goodsVoList);
        return "goodsList";
    }

    @GetMapping("/toDetail")
    public String toDetail(Model model, @RequestParam Long goodsId) {
        UserEntity user = UserContext.getUser();

        GoodsEntity goods = goodsService.get(goodsId);
        SeckillGoodsEntity seckillGoods = seckillGoodsService.get(goodsId);
        GoodsVo goodsVo = BeanUtil.copyProperties(goods, GoodsVo.class);
        goodsVo.setStartDate(seckillGoods.getStartDate());
        goodsVo.setEndDate(seckillGoods.getEndDate());
        goodsVo.setSeckillPrice(seckillGoods.getSeckillPrice());
        goodsVo.setStockCount(seckillGoods.getStockCount());

        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int seckillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;

        if (nowDate.before(startDate)) {
            //秒杀还未开始0
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        } else if (nowDate.after(endDate)) {
            //秒杀已经结束
            seckillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀进行中
            seckillStatus = 1;
            remainSeconds = 0;
        }

        model.addAttribute("goods", goodsVo);
        model.addAttribute("user", user);
        model.addAttribute("secKillStatus", seckillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        return "goodsDetail";
    }

    /**
     * 查询商品详情
     * @param goodsId
     * @return
     */
    @ResponseBody
    @GetMapping("/detail")
    public R<GoodsDetailVo> get(@RequestParam Long goodsId) {

        GoodsDetailVo goodsDetailVO = new GoodsDetailVo();

        GoodsEntity goods = goodsService.get(goodsId);
        SeckillGoodsEntity seckillGoods = seckillGoodsService.get(goodsId);
        GoodsVo goodsVo = BeanUtil.copyProperties(goods, GoodsVo.class);
        goodsVo.setStartDate(seckillGoods.getStartDate());
        goodsVo.setEndDate(seckillGoods.getEndDate());
        goodsVo.setSeckillPrice(seckillGoods.getSeckillPrice());
        goodsVo.setStockCount(seckillGoods.getStockCount());

        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int seckillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;

        if (nowDate.before(startDate)) {
            //秒杀还未开始0
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        } else if (nowDate.after(endDate)) {
            //秒杀已经结束
            seckillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀进行中
            seckillStatus = 1;
        }

        goodsDetailVO.setGoodsVo(goodsVo);
        goodsDetailVO.setUser(UserContext.getUser());
        goodsDetailVO.setRemainSeconds(remainSeconds);

        return R.success(goodsDetailVO);
    }

}
